package com.ram.service.impl;

import com.ram.mapper.RepairRequestMapper;
import com.ram.pojo.RepairRequest;
import com.ram.service.RepairRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import com.ram.pojo.PageResult;
import com.ram.pojo.RepairRequestQueryParam;

@Service
public class RepairRequestServiceImpl implements RepairRequestService {
    @Autowired
    private RepairRequestMapper mapper;
    @Override
    public List<RepairRequest> list() { return mapper.list(); }
    @Override
    public RepairRequest getById(Integer id) { return mapper.getById(id); }
    @Override
    public void add(RepairRequest request) { mapper.insert(request); }
    @Override
    public void update(RepairRequest request) { mapper.update(request); }
    @Override
    public void deleteById(Integer id) { mapper.deleteById(id); }

    @Override
    public PageResult pageQuery(RepairRequestQueryParam param) {
        List<RepairRequest> list = mapper.pageQuery(param);
        Long total = mapper.count(param);
        PageResult result = new PageResult();
        result.setTotal(total);
        result.setRecords(list);
        return result;
    }

    @Override
    public void audit(Integer id, Boolean pass, String remark) {
        RepairRequest req = mapper.getById(id);
        if (req == null) return;
        if (pass) {
            req.setStatus(1); // 待派单
        } else {
            req.setStatus(4); // 已驳回
            req.setRemark(remark);
        }
        req.setUpdateTime(java.time.LocalDateTime.now());
        mapper.update(req);
    }

    @Override
    public void assign(Integer id, Integer assigneeId, String assigneeName) {
        RepairRequest req = mapper.getById(id);
        if (req == null) return;
        req.setStatus(2); // 维修中
        req.setAssigneeId(assigneeId);
        req.setAssigneeName(assigneeName);
        req.setAssignTime(java.time.LocalDateTime.now());
        req.setUpdateTime(java.time.LocalDateTime.now());
        mapper.update(req);
    }

    @Override
    public void finish(Integer id) {
        RepairRequest req = mapper.getById(id);
        if (req == null) return;
        req.setStatus(3); // 已完成
        req.setFinishTime(java.time.LocalDateTime.now());
        req.setUpdateTime(java.time.LocalDateTime.now());
        mapper.update(req);
    }
} 